package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.activity.client.ActivityFeignClient;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.product.model.SkuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-08-16 10:15
 */
@Controller
public class SeckillHtmlController {

    @Autowired
    private ActivityFeignClient activityFeignClient;


    /**
     * 秒杀列表页渲染
     *
     * @param model
     * @return
     */
    @GetMapping("/seckill.html")
    public String seckillListHtml(Model model) {
        //1.远程调用秒杀服务获取秒杀商品列表
        Result result = activityFeignClient.getSeckillGoods();
        //2.渲染页面
        model.addAttribute("list", result.getData());
        return "/seckill/index";
    }

    @GetMapping("/seckill/{skuId}.html")
    public String seckillItemHtml(Model model, @PathVariable("skuId") Long skuId) {
        Result result = activityFeignClient.getSecGoodsById(skuId);
        model.addAttribute("item", result.getData());
        return "/seckill/item";
    }

    /**
     * 渲染排队页面
     *
     * @param model
     * @param skuId
     * @param buyCode
     * @return
     */
    @GetMapping("/seckill/queue.html")
    public String seckillQueueHtml(Model model, @RequestParam("skuId") Long skuId, @RequestParam("skuIdStr") String buyCode) {
        model.addAttribute("skuId", skuId);
        model.addAttribute("skuIdStr", buyCode);
        return "/seckill/queue";
    }


    /**
     * 渲染秒杀订单确认页面
     *
     * @param model
     * @param skuId 秒杀商品ID
     * @return
     */
    @GetMapping("/seckill/trade.html")
    public String seckillTradeHtml(HttpServletRequest request, Model model, @RequestParam("skuId") Long skuId) {
        //获取从网关路由转发来用户ID
        String userId = AuthContextHolder.getUserId(request);
        //远程调用秒杀服务获取汇总数据
        Result<Map> result = activityFeignClient.getSeckillOrderTradeData(userId, skuId);
        model.addAllAttributes(result.getData());
        return "/seckill/trade";
    }
}
